package cn.haoma.base.utils;

import android.content.Context;

import java.lang.reflect.Field;

/**
 * Created by ilioili on 2015/3/17.
 */
public class SizeUtil {
    final public static int dpToPx(int dp, Context context) {
        return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    /**
     * 获取通知栏高度
     *
     * @return
     */
    final public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            return context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
            return 75;
        }
    }

    final public static float pxToDp(int px, Context context) {
        return px / context.getResources().getDisplayMetrics().density;
    }
}
